package com.boesch.electroOffice.presentation.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;

@FacesConverter("LocalDateConverter")
public class LocalDateConverter implements Converter {
	
	private static final String PATTERN = "dd.MM.yyyy";

	public Object getAsObject(FacesContext context, UIComponent component, String value) {
        LocalDate date = null;
        try{
            date = DateTimeFormat.forPattern(PATTERN).parseLocalDate(value);
        }catch (Exception ex){
            throw new ConverterException(ex);
        }
        return date;
	}

	public String getAsString(FacesContext context, UIComponent component, Object value) {
        String date = null;
        try{
            date = DateTimeFormat.forPattern(PATTERN).print((LocalDate) value);
        }catch (Exception ex){
            throw  new ConverterException(ex);
        }
        return date;
	}
}
